package GameEngine;

import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;


public class Util {

	public static final GraphicsConfiguration CONFIG =
	    GraphicsEnvironment.getLocalGraphicsEnvironment().
		getDefaultScreenDevice().getDefaultConfiguration();

	
	public static void pause(long TempoMiliSeconds) {
		long t1 = System.currentTimeMillis() + TempoMiliSeconds;
		while (t1 > System.currentTimeMillis()) {
		}
	}

	public static int getRandom(int minimo, int maximo) {
		int resultado;
		double d = (Math.random() * (maximo - minimo + 1));
		resultado = (int) d + minimo;
		return resultado;
	}
	
	  public static BufferedImage rotateLeft(BufferedImage bi) 
	  { 
	    AffineTransform afLeft = AffineTransform.getRotateInstance( 
	        Math.toRadians(270)); 
	    afLeft.translate(bi.getWidth() * -1,0); 
	    AffineTransformOp lOp = new AffineTransformOp(afLeft,null); 

	    BufferedImage dstbi = lOp.filter(bi, null); 

	    return dstbi; 
	  } 

	  public static BufferedImage rotateRight(BufferedImage bi) 
	  { 
	    AffineTransform afRight = AffineTransform.getRotateInstance( 
	        Math.toRadians(90)); 
	    afRight.translate(0,bi.getHeight() * -1); 
	    AffineTransformOp rOp = new AffineTransformOp(afRight,null); 

	    BufferedImage dstbi = rOp.filter(bi, null); 

	    return dstbi; 
	  } 
	  
	  public static BufferedImage flipVertically(BufferedImage bi) {
		  	AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
		    tx.translate(0, -bi.getHeight(null));
		    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
		    return op.filter(bi, null);
	  }
	  public static BufferedImage flipHorizontally(BufferedImage bi) {
		  	AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
	   tx = AffineTransform.getScaleInstance(-1, 1);
	    tx.translate(-bi.getWidth(null), 0);
	    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
	    return op.filter(bi, null);
	  }
	  
	   public static BufferedImage rotate(BufferedImage bi) {
		   	AffineTransform transform = new AffineTransform();
	        transform.rotate(4,bi.getWidth()/2,bi.getHeight()/2);
	        AffineTransformOp op = new AffineTransformOp(transform,AffineTransformOp.TYPE_BILINEAR);
	        BufferedImage ret = op.filter(bi,null);
	        return ret;
	    }
	   
	   public static BufferedImage rotate(BufferedImage src, int angle) {
			int w = src.getWidth(),
				h = src.getHeight(),
				transparency = src.getColorModel().getTransparency();
			BufferedImage image = createImage(w, h, transparency);

			Graphics2D g = image.createGraphics();
			g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
							   RenderingHints.VALUE_INTERPOLATION_BILINEAR);
			g.rotate(Math.toRadians(angle), w/2, h/2);
			g.drawImage(src, 0, 0, null);
			g.dispose();

			return image;
		}
	   public static BufferedImage createImage(int width, int height,
				int transparency) {
		   //return new BufferedImage(width, height, transparency);
		   return CONFIG.createCompatibleImage(width, height, transparency);
}


}
